package com.devshephard.jucks.SingleDemo;

/**
 * 懒汉式单例模式,单线程下线程安全，多线程不安全
 */
public class LazyMan {

    //单例模式必须构造器私有
    private LazyMan() {
        System.out.println(Thread.currentThread().getName() + "ok");  //多线程情况下输出一下当前线程,由于放在了构造器中,如果有多个线程输出则表明生成了多个对象
    }

    private static LazyMan lazyMan;

    public static LazyMan getInstance(){
      if(lazyMan == null){ //当lazyMan对象为空,则表明没有生成到内存空间中
          lazyMan = new LazyMan();
      }
      return lazyMan;
    };


    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {  //模拟多线程情况下线程不安全
            new Thread(()->{
                LazyMan.getInstance();
            }).start();
        }
    }
}
